﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Retirement_fundSwitchPremiumRedirection : System.Web.UI.Page
{
    Dictionary<string, string> sourcePercentageOrAmount = new Dictionary<string, string>();
    Dictionary<string, string> targetPercentage = new Dictionary<string, string>();
    Dictionary<string, string> allocationPercentage = new Dictionary<string, string>();


    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_fundSwitchRequest_Click(object sender, ImageClickEventArgs e)
    {
        int custId = -1;

        if (Session["userID"] == null)
        {
            custId = 15369472;//TODO GET FROM SESSION

        }
        else
        {
            custId = Convert.ToInt32(Session["userID"]);

        }

        List<Policy> policies = Policy.getPolicy(custId);
        Policy policy = policies.ElementAt(0);

        Requests requests = new Requests();

        if (cb_metRepresentative.Checked)
        {
            requests.metRepresentative = "Y";
        }else{
            requests.metRepresentative = "N";
        }

        requests.policyNo = policy.policyNo;

        try
        {
        this.getSwitchFundRequest();
        this.getRedirectFundRequest();

        if(!this.cb_agree.Checked)
        {
            throw new RetirementException(RetirementException.CHECK_BOX_UNCHECKED);
        }

        //public static int FUND_SWITCH_MIN_PER_FUND = 400;//Minimum Switch Out amount per fund is $500 or entire fund value(100%) whichever lower.
        //public static int MAX_FUND_NUMBER_PER_POLICY = 401;//Maximum no. of funds per policy is 5.

            requests.fundSwitchRedirectRequest(sourcePercentageOrAmount, targetPercentage, allocationPercentage);

            this.Response.Redirect("./requestSuccessful.aspx?");

        }
        catch (RetirementException ex)
        {
            this.lb_exceptionMsg.Text = ex.message;
        }

    }

    protected void btn_cancelRequest_Click(object sender, ImageClickEventArgs e)
    {
        this.Response.Redirect("./myPortfolio.aspx");
    }

    private void getSwitchFundRequest()
    {

        if (!Util.IsBlank(ddl_sourceFund1.SelectedValue))
        {
            sourcePercentageOrAmount.Add(ddl_sourceFund1.SelectedValue, this.tb_sourceFundAmount1.Text);
        }

        if (!Util.IsBlank(ddl_sourceFund2.SelectedValue))
        {
            sourcePercentageOrAmount.Add(ddl_sourceFund2.SelectedValue, this.tb_sourceFundAmount2.Text);
        }

        if (!Util.IsBlank(ddl_sourceFund3.SelectedValue))
        {
            sourcePercentageOrAmount.Add(ddl_sourceFund3.SelectedValue, this.tb_sourceFundAmount3.Text);
        }

        if (!Util.IsBlank(ddl_sourceFund4.SelectedValue))
        {
            sourcePercentageOrAmount.Add(ddl_sourceFund4.SelectedValue, this.tb_sourceFundAmount4.Text);
        }

        if (!Util.IsBlank(ddl_sourceFund5.SelectedValue))
        {
            sourcePercentageOrAmount.Add(ddl_sourceFund5.SelectedValue, this.tb_sourceFundAmount5.Text);
        }

        
        //targetPercentage
        if (!Util.IsBlank(ddl_targetFund1.SelectedValue))
        {
            targetPercentage.Add(ddl_targetFund1.SelectedValue, this.tb_targetFundAmount1.Text);
        }

        if (!Util.IsBlank(ddl_targetFund2.SelectedValue))
        {
            targetPercentage.Add(ddl_targetFund2.SelectedValue, this.tb_targetFundAmount2.Text);
        }

        if (!Util.IsBlank(ddl_targetFund3.SelectedValue))
        {
            targetPercentage.Add(ddl_targetFund3.SelectedValue, this.tb_targetFundAmount3.Text);
        }

        if (!Util.IsBlank(ddl_targetFund4.SelectedValue))
        {
            targetPercentage.Add(ddl_targetFund4.SelectedValue, this.tb_targetFundAmount4.Text);
        }

        if (!Util.IsBlank(ddl_targetFund5.SelectedValue))
        {
            targetPercentage.Add(ddl_targetFund5.SelectedValue, this.tb_targetFundAmount5.Text);
        }

        PolicyRules policyRules = new PolicyRules();

        if (sourcePercentageOrAmount.Keys.Count == 0 && targetPercentage.Keys.Count == 0)
        {
            return;
        }

        decimal totalPercentage = 0;
        foreach (string percentage in targetPercentage.Values)
        {
            totalPercentage += Convert.ToDecimal(percentage);
        }

        if (totalPercentage != 100)
        {   
            //Total percentage allocation for Fund Switch Target must be 100%
            throw new RetirementException(RetirementException.FUND_SWITCH_TARGET_TOTAL_PERCENTAGE);
        }

      //  policyRules.switchFund(policyNo, sourcePercentageOrAmount, targetPercentage);

    }
    

    private void getRedirectFundRequest()
    {

        //Redirect funds
        if (!Util.IsBlank(ddl_redirectFund1.SelectedValue))
        {
            allocationPercentage.Add(ddl_redirectFund1.SelectedValue, this.tb_redirectFundPercentage1.Text);
        }

        if (!Util.IsBlank(ddl_redirectFund2.SelectedValue))
        {
            allocationPercentage.Add(ddl_redirectFund2.SelectedValue, this.tb_redirectFundPercentage2.Text);
        }

        if (!Util.IsBlank(ddl_redirectFund3.SelectedValue))
        {
            allocationPercentage.Add(ddl_redirectFund3.SelectedValue, this.tb_redirectFundPercentage3.Text);
        }

        if (!Util.IsBlank(ddl_redirectFund4.SelectedValue))
        {
            allocationPercentage.Add(ddl_redirectFund4.SelectedValue, this.tb_redirectFundPercentage4.Text);
        }

        if (!Util.IsBlank(ddl_redirectFund5.SelectedValue))
        {
            allocationPercentage.Add(ddl_redirectFund5.SelectedValue, this.tb_redirectFundPercentage5.Text);
        }

        if (allocationPercentage.Keys.Count == 0 && allocationPercentage.Keys.Count == 0)
        {
            return;
        }

        PolicyRules policyRules = new PolicyRules();

        decimal totalRedirectPercentage = 0;
        foreach (string percentage in allocationPercentage.Values)
        {
            totalRedirectPercentage += Convert.ToDecimal(percentage);
        }

        if (totalRedirectPercentage != 100)
        {
            //Total percentage allocation for PREMIUM REDIRECTION must be 100%
            throw new RetirementException(RetirementException.REDIRECT_FUND_TOTAL_PERCENTAGE);
        }

    //    policyRules.redirectFund(policyNo, allocationPercentage);

    }



    protected void cb_agree_CheckedChanged(object sender, EventArgs e)
    {

    }
}